package dao

import (
	"context"
	"gorm.io/gorm"
)

// FeedPushEvent 推
type FeedPushEvent struct {
	Id      int64  `gorm:"primaryKey,autoIncrement"`
	Uid     int64  `gorm:"type:int(11)"`
	Type    string `gorm:"type:varchar(255)"`
	Content string `gorm:"type:text"`

	Ctime int64 `gorm:"index"`
}

func (FeedPushEvent) TableName() string {
	return "feed_push_event"
}

type FeedPushEventDAO interface {
	CreatePushEvents(ctx context.Context, fp []FeedPushEvent) error
	FindPushEventList(ctx context.Context, uid int64, timestamp, limit int64) ([]FeedPushEvent, error)
	FindPushEventListWithTyp(ctx context.Context, typ string, uids int64, timestamp,
		limit int64) ([]FeedPushEvent, error)
}
type feedPushEventDAO struct {
	db *gorm.DB
}

func NewFeedPushEventDAO(db *gorm.DB) FeedPushEventDAO {
	return &feedPushEventDAO{db: db}
}

func (f feedPushEventDAO) CreatePushEvents(ctx context.Context, fp []FeedPushEvent) error {
	return f.db.WithContext(ctx).Create(&fp).Error
}
func (f feedPushEventDAO) FindPushEventList(ctx context.Context, uids int64, timestamp,
	limit int64) ([]FeedPushEvent, error) {
	var fp []FeedPushEvent
	err := f.db.WithContext(ctx).Where("uid = ? and ctime>?", uids, timestamp).Limit(int(limit)).
		Find(&fp).Error
	return fp, err
}
func (f feedPushEventDAO) FindPushEventListWithTyp(ctx context.Context, typ string, uids int64, timestamp,
	limit int64) ([]FeedPushEvent, error) {
	var fp []FeedPushEvent
	err := f.db.WithContext(ctx).Where("uid = ? and ctime>? and type=?", uids, timestamp, typ).
		Limit(int(limit)).Find(&fp).Error
	return fp, err
}
